import pygame


class Inventory:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.sprites = []  # 存储精灵列表
        
        # 初始化背包格子相关参数
        self.slot_size = 60  # 格子大小
        self.slot_padding = 10  # 格子间距
        self.slots_per_row = 8  # 每行格子数
        
        # 初始化选中精灵和属性框
        self.selected_sprite = None
        self.stats_box = pygame.Rect(500, 100, 200, 200)  # 属性框位置和大小
        self.font = pygame.font.Font(None, 24)  # 属性文本字体
    
    def clear(self):
        """清空背包中的所有精灵"""
        self.sprites = []
        
    def add_sprite(self, sprite):
        """添加精灵到背包"""
        if sprite not in self.sprites:
            self.sprites.append(sprite)
        
    def draw(self, screen):
        # 绘制背包格子
        for i, sprite in enumerate(self.sprites):
            row = i // self.slots_per_row
            col = i % self.slots_per_row
            x = 50 + col * (self.slot_size + self.slot_padding)
            y = 100 + row * (self.slot_size + self.slot_padding)
            
            # 绘制格子背景
            slot_rect = pygame.Rect(x, y, self.slot_size, self.slot_size)
            pygame.draw.rect(screen, (200, 200, 200), slot_rect)
            pygame.draw.rect(screen, (100, 100, 100), slot_rect, 2)
            
            # 绘制精灵图片
            sprite_image = pygame.transform.scale(sprite.image, (self.slot_size-10, self.slot_size-10))
            screen.blit(sprite_image, (x+5, y+5))
            
        # 如果有选中的精灵，显示其属性
        if self.selected_sprite:
            self.draw_sprite_stats(screen)
    
    def draw_sprite_stats(self, screen):
        # 确保有选中的精灵才绘制属性框
        if not self.selected_sprite:
            return
            
        pygame.draw.rect(screen, (220, 220, 220), self.stats_box)
        pygame.draw.rect(screen, (100, 100, 100), self.stats_box, 2)
        
        try:
            stats = [
                f"NAME: {self.selected_sprite.name}",
                f"HP: {self.selected_sprite.max_hp}",
                f"ATK: {self.selected_sprite.attack}",
                f"DEF: {self.selected_sprite.defense}",
                f"SPD: {self.selected_sprite.speed}"
            ]
            
            for i, stat in enumerate(stats):
                text = self.font.render(stat, True, (0, 0, 0))
                screen.blit(text, (self.stats_box.x + 10, self.stats_box.y + 10 + i * 30))
        except AttributeError:
            self.selected_sprite = None
    
    def handle_click(self, pos):
        # 检查是否点击了精灵格子
        for i, sprite in enumerate(self.sprites):
            row = i // self.slots_per_row
            col = i % self.slots_per_row
            x = 50 + col * (self.slot_size + self.slot_padding)
            y = 100 + row * (self.slot_size + self.slot_padding)
            
            slot_rect = pygame.Rect(x, y, self.slot_size, self.slot_size)
            if slot_rect.collidepoint(pos):
                self.selected_sprite = sprite
                return True
        return False